/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ggsoft.timetracker.utils;

import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 * @date Jan 6, 2011
 * @author Ggutierrez
 */
public class ObjectLookup {

    private SessionFactory sessionFactory;

    /**
     * Creates a new instance of ObjectLookup
     */
    public ObjectLookup() {
    }

    public ObjectLookup(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Object find(Object id, Class clazz) {
        Session sess = null;
        try {
            sess = sessionFactory.getCurrentSession();
        } catch (Exception e) {
            sess = sessionFactory.openSession();
        }

        if (sess != null) {
            Object obj = sess.get(clazz, (Serializable) id);
            sess.flush();
            return obj;
        } else {
            return null;
        }
    }
}
